<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of villageAction
 *
 * @author xlp
 */
class villageAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->openStatus = array('未开通', '<span class="red">已开通</span>');
        $this->stype = array('小区', '写字楼');
        $this->serviceData = parent::getServiceCache();
    }

    function index() {
        $title = $this->_get('q');
        $areaId = $this->_getid('area_id', 0);
        $streetId = $this->_getid('street_id', 0);
        $communityId = $this->_getid('community_id', 0);
        $serviceId = $this->_getid('service_id', 0);
        $zoneId = $this->_getid('zone_id', 0);
        $isOpen = $this->_getid('is_open', 0);
        $stype = $this->_getid('type', 0);
        $p = $this->_getid('p', 1);
        $order = $this->_get('order', 'vid');
        $pageShow = 20;
        $limit = ($p - 1) * $pageShow . ',' . $pageShow;
        $where = array('AND' => array('city_id' => admin::$adminInfo['city_id']));
        if ($title) {
            $where['LIKE'] = array('ctitle' => parent::safeSearch($title));
        }
        if ($areaId) {
            $where['AND']['area_id'] = $areaId;
        }
        if ($streetId) {
            $where['AND']['street_id'] = $streetId;
        }
        if ($communityId) {
            $where['AND']['community_id'] = $communityId;
        }
        if ($serviceId) {
            $where['AND']['service_id'] = $serviceId;
        }
        if ($zoneId) {
            $where['AND']['zone_id'] = $zoneId;
        }
        if ($isOpen) {
            $where['AND']['is_open'] = ($isOpen - 1);
        }
        if ($stype) {
            $where['AND']['stype'] = ($stype - 1);
        }
        if (!in_array($order, array('vid', 'count'))) {
            $order = 'vid';
        }
//        $rs = M('village')->where($where)->page($p)->order('vid DESC')->findAll();
        $rs = M('village')->query('SELECT a.*,b.count FROM __TABLE__ AS a '
                . 'LEFT JOIN (SELECT village_id,COUNT(uid) AS count FROM __PRE__user WHERE village_id>0 GROUP BY village_id) AS b '
                . 'ON a.vid=b.village_id ' . M('village')->getWhere($where) . ' ORDER BY ' . $order . ' DESC LIMIT ' . $limit);
        $total = M('village')->where($where)->count();
//        M('village')->getAllSql();
//        z($rs);
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['city_name'] = parent::getCityName($v['city_id']);
                $rs[$k]['area_name'] = parent::getAreaName($v['area_id']);
                $rs[$k]['street_name'] = parent::getAreaName($v['street_id']);
                $rs[$k]['community_name'] = parent::getAreaName($v['community_id']);
                $rs[$k]['business_zone_name'] = parent::getBusinessZoneName($v['zone_id']);
                $rs[$k]['service_name'] = isset($this->serviceData[$v['service_id']]) ? $this->serviceData[$v['service_id']]['stitle'] : '';
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'area_id' => $areaId,
            'street_id' => $streetId, 'service_id' => $serviceId, 'community_id' => $communityId,
            'title' => $title, 'openstatus' => $this->openStatus, 'stype' => $this->stype, 'pageShow' => $pageShow));
//        print_r($this->getAllVar());
//        z($rs);
        $this->display();
    }

    function add() {
        $areaId = $this->_getid('area_id', 0);
        $streetId = $this->_getid('street_id', 0);
        $communityId = $this->_getid('community_id', 0);
        $businessZone = M('business_zone')->where(array('city_id' => admin::$adminInfo['city_id']))->findAll();
        $service = $this->serviceData;
        $this->assign(array('area_id' => $areaId, 'street_id' => $streetId, 'community_id' => $communityId,
            'openstatus' => $this->openStatus, 'businessZone' => $businessZone, 'stype' => $this->stype,
            'service' => $service
        ));
        $this->display();
    }

    function post() {
//        z($_POST);
        $objData = array();
        //获取字段内容
        $fieldList = M('village')->getTableFields();
        foreach ($fieldList['fields'] as $key => $val) {
            $objData[$key] = $val['type'] == 'int' ? $this->_postid($key, $val['value']) : $this->_post($key, $val['value']);
        }
        $objData['city_id'] = admin::$adminInfo['city_id'];
        $objData['fpath'] = $objData['area_id'] . ',' . $objData['street_id'] . ',' . $objData['community_id'];
        if (!$objData['title']) {
            $this->JsonReturn('小区名称不能为空');
        }
        if (!$objData['ctitle']) {
            $objData['ctitle'] = $objData['title'];
        } else {
            $objData['ctitle'] = str_replace(array('，', ' ', '、', '|'), ',', $objData['ctitle']);
        }
        //判断重复
        if (M('village')->where(array('title' => $objData['title'], 'city_id' => $objData['city_id']))->find()) {
            $this->JsonReturn('同名小区已存在，请检查');
        }
//        z($objData);
        M('village')->insert($objData);
        $this->JsonReturn('ok', null, 1);
    }

    function edit() {
        $id = $this->_getid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $rs = M('village')->where(array('vid' => $id))->find();
        if (!$rs) {
            showError('小区不存在');
        }
        $rs['area_name'] = $rs['area_id'] ? parent::getAreaName($rs['area_id']) : '';
        $rs['street_name'] = $rs['street_id'] ? parent::getAreaName($rs['street_id']) : '';
        $rs['community_name'] = $rs['community_id'] ? parent::getAreaName($rs['community_id']) : '';
        $rs['city_name'] = $rs['city_id'] ? parent::getCityName($rs['city_id']) : '';
        $businessZone = M('business_zone')->where(array('city_id' => admin::$adminInfo['city_id']))->findAll();
        $service = $this->serviceData;
//        z($rs);
        $this->assign(array('rs' => $rs, 'openstatus' => $this->openStatus, 'businessZone' => $businessZone, 'stype' => $this->stype,
            'service' => $service
        ));
        $this->display();
    }

    function save() {
//        z($_POST);
        $id = $this->_postid('vid', 0);
        if (!$id) {
            $this->JsonReturn('参数丢失');
        }
        $objData = array();
        $objData['title'] = $this->_post('title');
        $objData['ctitle'] = $this->_post('ctitle');
        $objData['pic_url'] = $this->_post('pic_url');
        $objData['area_id'] = $this->_postid('area_id');
        $objData['street_id'] = $this->_postid('street_id');
        $objData['community_id'] = $this->_postid('community_id');
        $objData['is_open'] = $this->_postid('is_open');
        $objData['zone_id'] = $this->_postid('zone_id');
        $objData['service_id'] = $this->_postid('service_id');
        $objData['stype'] = $this->_postid('stype');
        $objData['lng'] = $this->_post('lng');
        $objData['lat'] = $this->_post('lat');
        $objData['address'] = $this->_post('address');
        if (!$objData['title']) {
            $this->JsonReturn('小区名称不能为空');
        }
        if (!$objData['ctitle']) {
            $objData['ctitle'] = $objData['title'];
        } else {
            $objData['ctitle'] = str_replace(array('，', ' ', '、', '|'), ',', $objData['ctitle']);
        }
        $rs = M('village')->where(array('vid' => $id))->find();
        if (!$rs) {
            showError('小区不存在');
        }
        $objData['fpath'] = $objData['area_id'] . ',' . $objData['street_id'] . ',' . $objData['community_id'];
        //临时增加=====正式版请删除
        $objData['is_edit'] = 1;

        M('village')->update($objData, array('vid' => $id, 'city_id' => admin::$adminInfo['city_id']));
        $this->JsonReturn('ok', null, 1);
    }

    function delarea() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        M('village')->update(array('area_id' => 0, 'street_id' => 0, 'community_id' => 0), array('vid' => $id, 'city_id' => admin::$adminInfo['city_id']));
        //DOTO:清除相关数据
        $this->JsonReturn('ok', null, 1);
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查该小区下是否有绑定的用户
        $checkUser = D('member')->where(array('village_id' => $id))->count();
        if ($checkUser) {
            showError('该小区下已有用户入住，不可删除');
        }
        M('village')->delete(array('vid' => $id, 'city_id' => admin::$adminInfo['city_id']));
        //DOTO:清除相关数据
        $this->JsonReturn('ok', null, 1);
    }

    function setcommunity() {
        $vid = $this->_postid('vid', 0);
        $communityId = $this->_postid('community_id', 0);
        $objData = array(
            'community_id' => $communityId
        );
        if (!$vid) {
            showError('参数丢失');
        }
        $rs = M('village')->where(array('vid' => $vid, 'city_id' => admin::$adminInfo['city_id']))->find();
        if (!$rs) {
            showError('小区不存在');
        }
        $objData['street_id'] = M('area')->where(array('aid' => $communityId, 'city_id' => admin::$adminInfo['city_id']))->getField('fid');
        if (!$objData['street_id']) {
            showError('街道ID不正确');
        }
        $objData['area_id'] = M('area')->where(array('aid' => $objData['street_id'], 'city_id' => admin::$adminInfo['city_id']))->getField('fid');
        if (!$objData['street_id']) {
            showError('城区ID不正确');
        }
        $objData['fpath'] = $objData['area_id'] . ',' . $objData['street_id'] . ',' . $objData['community_id'];
//        z($objData);
        M('village')->update($objData, array('vid' => $vid, 'city_id' => admin::$adminInfo['city_id']));
        $this->JsonReturn('ok', null, 1);
    }

    function search() {
        $type = $this->_getid('type', 0);
        $areaId = $this->_getid('area_id', 0);
        $streetId = $this->_getid('street_id', 0);
        $communityId = $this->_getid('community_id', 0);
        $this->assign(array('type' => $type, 'area_id' => $areaId, 'street_id' => $streetId, 'community_id' => $communityId));
        $this->display();
    }

    function ajax() {
        $title = $this->_get('title');
        $p = $this->_getid('p', 1);
        $areaId = $this->_getid('area_id', 0);
        $streetId = $this->_getid('street_id', 0);
        $communityId = $this->_getid('community_id', 0);
        $where = array('LIKE' => array('title' => parent::safeSearch($title)), 'AND' => array('city_id' => admin::$adminInfo['city_id']));
        if (!$title) {
            showError('请输入关键字');
        }
        if ($areaId) {
            $where['AND']['area_id'] = $areaId;
        }
        if ($streetId) {
            $where['AND']['street_id'] = $streetId;
        }
        if ($communityId) {
            $where['AND']['community_id'] = $communityId;
        }
        $rs = M('village')->where($where)->page($p, 5)->findAll();
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['area_name'] = $v['area_id'] ? parent::getAreaName($v['area_id']) : '';
                $rs[$k]['street_name'] = $v['street_id'] ? parent::getAreaName($v['street_id']) : '';
                $rs[$k]['community_name'] = $v['community_id'] ? parent::getAreaName($v['community_id']) : '';
            }
        }
//        z(M('area')->getAllSql());
        $this->assign(array('rs' => $rs, 'total' => M('village')->getTotal(), 'p' => $p));
        $this->JsonReturn('ok', $this->getFetch(), 1);
    }

    /*
     * 获取第三方绑定链接
     */

    function geturl() {
        $id = $this->_getid('id', 0);
        if (!$id) {
            showError('参数丢失');
        }
        $this->assign(array('id' => $id));
        $this->display();
    }

}
